package com.yyk.yykywd.mvvm.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.yyk.yykywd.R
import com.yyk.yykywd.config.HttpUrl
import com.yyk.yykywd.util.GlideUtils
import java.io.File

/**
 * 文件描述：
 * Created by guo on 2019-11-18.
 */
class AddMaterialAdapter(private val context: Context, private val list: MutableList<String>) : RecyclerView.Adapter<AddMaterialAdapter.ViewHolder>() {
    private var mListener: ((Int, View) -> Unit)? = null
    var maxImages = 4

    //lambda表达式作为函数中的参数
    fun setOnItemChildClickListener(mListener: (position: Int, view: View) -> Unit) {
        this.mListener = mListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_add_material, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        if (position < list.size) {
            GlideUtils.loadRoundCircleImage(context, HttpUrl.BASE_URL + list[position], holder.iv, 4)
            holder.ivRemove.visibility = View.VISIBLE
            holder.iv.isClickable = false
        } else {
            GlideUtils.loadResourceImage(context, R.drawable.pic_tjzp, holder.iv)
            holder.ivRemove.visibility = View.INVISIBLE
            holder.iv.isClickable = true
            holder.iv.setOnClickListener {
                mListener?.invoke(position, it)
            }
        }
        holder.ivRemove.setOnClickListener {
            mListener?.invoke(position, it)
        }
    }

    override fun getItemCount(): Int {
        val count = list.size + 1
        return if (count > maxImages) {
            list.size
        } else {
            count
        }
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val iv: ImageView = itemView.findViewById(R.id.iv)
        val ivRemove: ImageView = itemView.findViewById(R.id.iv_remove)
    }

}